# noise handshake xx android ios step-counting aead crypto
**FIXED: Critical Noise Handshake Step Counting Bug**
**Problem**: Android was failing on step 2 of XX handshake with AEADBadTagException when acting as initiator. The logs showed "Unknown handshake step 1 for initiator" when receiving the 96-byte response from macOS.

**Root Cause**: The handshake step counting logic was incorrect. When the initiator (Android) received message 2 from the responder (macOS), the `handshakeMessageCount` was still 1 (from `startHandshake()`), but the validation expected it to be 2.

**Fix Applied**:
1. **Corrected Step Calculation**: Added logic to calculate `currentStep` properly before processing:
   ```kotlin
   val currentStep = if (isInitiator) {
       handshakeMessageCount + 1  // Initiator receiving message 2, 3, etc.
   } else {
       handshakeMessageCount      // Responder receiving message 1, 2, etc.
   }
   ```

2. **Enhanced Message Size Validation**: Made validation more lenient to accept larger messages (iOS might include additional payload):
   ```kotlin
   // More lenient validation - allow larger messages (they may contain additional payload)
   if (message.size < expectedSize) {
       Log.w(TAG, "Handshake message too small: got ${message.size}, minimum expected $expectedSize for step $step")
   } else if (message.size != expectedSize) {
       Log.d(TAG, "Handshake message size: got ${message.size}, expected $expectedSize for step $step (accepting larger message)")
   }
   ```

**Expected Result**: The XX handshake should now complete successfully between Android (initiator) and macOS/iOS (responder).

**Files Modified**: 
- `/app/src/main/java/com/bitchat/android/noise/NoiseSession.kt` - `processHandshakeMessage()` and `validateHandshakeMessageSize()` methods

**Build Status**: ✅ Successful compilation with `./gradlew assembleDebug`

